package UI
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.filters.GlowFilter;	
	
	public class UIButton extends Sprite
	{
		public var mBGColor:int=0xEEEEEE;
		public var mCaptionColor:int=0x000000;
		public var mCaption:TextField=null;
		public var mClickFunc:Function=null;
		public var mWidth:Number=0;
		public var mHeight:Number=0;
		public function UIButton()
		{
			super();
		}
		
		public function Create(x:int,y:int,w:int,h:int,caption:String,clickFunc:Function):Boolean
		{
			this.x=x;
			this.y=y;
			mWidth=w;
			mHeight=h;
			alpha=0.75;
			
			mCaption=new TextField;
			addChild(mCaption);
			mCaption.defaultTextFormat=UIManager.msInstance.mUITextFormat;
			mCaption.textColor=mCaptionColor;
			mCaption.selectable=false;
			mCaption.cacheAsBitmap=true;
			//mCaption.filters = [new GlowFilter(0x000000,1,1.2,1.2,100)];
			SetCaption(caption);
			
			mClickFunc=clickFunc;
			
			addEventListener(MouseEvent.MOUSE_DOWN,OnMouseDown);
			addEventListener(MouseEvent.MOUSE_UP,OnMouseUp);			
			addEventListener(MouseEvent.MOUSE_OVER,OnMouseMove);
			addEventListener(MouseEvent.MOUSE_OUT,OnMouseOut);
			
			//绘制按钮
			graphics.beginFill(mBGColor, 0.5);
			graphics.drawRoundRect(0,0,mWidth,mHeight,16,16);
			graphics.endFill();
			return true;
		}
		
		public function SetCaption(caption:String):void
		{
			mCaption.text=caption;
			mCaption.width=mCaption.textWidth+4;
			mCaption.height=mCaption.textHeight+4;
			mCaption.x=mWidth/2-mCaption.textWidth/2-2;
			mCaption.y=mHeight/2-mCaption.textHeight/2-2;
		}
	
		public function OnMouseUp(event:MouseEvent):void
		{
			this.alpha=0.9;
		}
		
		public function OnMouseDown(event:MouseEvent):void
		{
			this.alpha = 1;
			
			if(mClickFunc!=null)
				mClickFunc();
			event.stopImmediatePropagation();
		}
		
		public function OnMouseMove(event:MouseEvent):void
		{
			this.alpha=0.9;
		}
		public function OnMouseOut(event:MouseEvent):void
		{
			this.alpha=0.75;
		}
	}
}